<?php


namespace app\helpers\traits;

use app\api\model\Image;
use app\api\model\PlaceType;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\facade\Env;

trait ImageUpload
{
    /**
     * @param $file    //文件
     * @param $folder  //文件夹名称
     * 上传图片
     * @return array
     */
    public function save($file, $folder)
    {
        $saveName = \think\facade\Filesystem::disk('public')
            ->putFile($folder, $file);
        $path = str_replace('\\', '/', $saveName);
        return [
            'path' => $path,
        ];
    }


    //导入
    public function saveImportExcel($file, $folder)
    {
        $saveName = \think\facade\Filesystem::disk('public')->putFile($folder, $file);
        $path = app()->getRootPath() . 'public/storage/' . str_replace('\\', '/', $saveName);
        $returnData = $this->loadExcel($path);
        $data = $this->dadaFilter($returnData);
        return $data;
    }

    //创建文件夹
    public function mkdirs($dir, $mode = 0777)
    {
        if (is_dir($dir) || @mkdir($dir, $mode)) {
            return true;
        }
        if (!mkdir(dirname($dir), $mode)) {
            return false;
        }
        return @mkdir($dir, $mode);
    }


    //载入excel
    public function loadExcel($filePath)
    {
        $inputFileType = IOFactory::identify($filePath);
        $reader = IOFactory::createReader($inputFileType);
        $reader->setReadDataOnly(TRUE);
        $spreadsheet = $reader->load($filePath); //载入excel表格
        $res_arr = array();
        $reader = $spreadsheet->getWorksheetIterator();
        foreach ($reader as $sheet) {
            //读取表内容
            $content = $sheet->getRowIterator();
            //逐行处理
            foreach ($content as $key => $items) {
                $rows = $items->getRowIndex();              //行
                $columns = $items->getCellIterator();       //列
                $row_arr = array();
                //确定从哪一行开始读取
                if ($rows < 2) {
                    continue;
                }
                //逐列读取
                foreach ($columns as $head => $cell) {

                    $data = $cell->getValue();

                    $row_arr[] = $data;
                }

                array_push($res_arr, array_values(array_filter($row_arr, function ($var) {
                    return $var !== null;
                })));

            }
        }
        return array_filter($res_arr);
    }

    //处理excel
    public function dadaFilter(array $array)
    {
        $filterData = [];
        $k1 = $i1 = $j1 = $k2 = $i2 = $i3 = $i4 = 0;
        foreach ($array as $data) {
            if (count($data) == 6) {
                $i1 = $j1 = 0;
                $filterData[$k1] = ['issue_title' => $data[0]];
                $filterData[$k1]['item'][$i1] = [
                    'issue_question_title' => $data[1],
                    'issue_question_type' => $data[2],
                ];
                $filterData[$k1]['item'][$i1]['item'][] = [
                    'issue_answer_title' => $data[3],
                    'issue_answer_type' => $data[4],
                    'is_write' => $data[5],
                ];
                $k2 = $k1;
                $i2 = $i1;
                $i4 = $i3 = $j1;
                $k1++;
                $i1++;
                $j1++;
            }

           if (count($data) == 5) {
                $filterData[$k2]['item'][$i1] = [
                    'issue_question_title' => $data[0],
                    'issue_question_type' => $data[1],

                ];
                $filterData[$k2]['item'][$i1]['item'][] = [
                    'issue_answer_title' => $data[2],
                    'issue_answer_type' => $data[3],
                    'is_write' => $data[4],
                ];
                $i2 = $i1;
                $i4++;
                $i1++;
            }

            if (count($data) == 3) {
                $filterData[$k2]['item'][$i2]['item'][] = [
                    'issue_answer_title' => $data[0],
                    'issue_answer_type' => $data[1],
                    'is_write' => $data[2],
                ];
                $i3++;
                $j1++;
            }

        }
        return $filterData;
    }


    //下载excel 模板
    public function downloadExcel($file_path)
    {
        header("Content-type:text/html;charset=utf-8");
        if (!file_exists($file_path)) {
            echo "没有该文件文件";
            return;
        }
        $fp = fopen($file_path, "r");
        $file_size = filesize($file_path);
        //下载文件需要用到的头
        Header("Content-type: application/octet-stream");
        Header("Accept-Ranges: bytes");
        Header("Accept-Length:" . $file_size);
        Header("Content-Disposition: attachment; filename=" . $file_path);
        $buffer = 1024;
        $file_count = 0;
        //向浏览器返回数据
        while (!feof($fp) && $file_count < $file_size) {
            $file_con = fread($fp, $buffer);
            $file_count += $buffer;
            echo $file_con;
        }
        fclose($fp);

    }
}
